package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.scenic.domain.ScenicTheme;
import cn.wolfcode.luowowo.scenic.domain.Ticket;
import cn.wolfcode.luowowo.scenic.domain.TicketDetail;
import cn.wolfcode.luowowo.scenic.service.IScenicDetailService;
import cn.wolfcode.luowowo.scenic.service.IScenicThemeService;
import cn.wolfcode.luowowo.scenic.service.ITicketDetailService;
import cn.wolfcode.luowowo.scenic.service.ITicketService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Base64;
import java.util.List;

@RequestMapping("/ticket")
@Controller
public class TicketController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicThemeService scenicThemeService;
    @Reference
    private IScenicDetailService scenicDetailService;
    @Reference
    private ITicketService ticketService;
    @Reference
    private ITicketDetailService ticketDetailService;

    @RequestMapping("")
    public Object index(Model model){
        //首页右侧城市
        List<Destination> dests = destinationService.listByGuangDongR10();
        model.addAttribute("dests", dests);
        //默认查询第一个城市的主题
        Destination destination = dests.get(0);
        List<ScenicTheme> themes = scenicThemeService.listByDestId(destination.getId());
        model.addAttribute("themes", themes);
        //默认查询第一个城市的主题的门票/景点信息
        List<Ticket> ticket = ticketService.listByDestIdAndThemeId(destination.getId(),themes.get(0).getId());
        model.addAttribute("tickets",ticket);

        //查询点评最多的4个景点
        model.addAttribute("hotTickets",ticketService.listHotTicketTop4());

        return "ticket/index";
    }


    @RequestMapping("/reloadSubAndProd")
    public Object reloadSubAndProd(Long ajaxDestId,Model model){
        //根据目的地id查询景点主题
        List<ScenicTheme> themes = scenicThemeService.listByDestId(ajaxDestId);
        model.addAttribute("themes", themes);
        //默认查询目的地下 第一个主题下的门票
        List<Ticket> ticket = ticketService.listByDestIdAndThemeId(ajaxDestId,themes.get(0).getId());
        model.addAttribute("tickets",ticket);

        return "ticket/reloadSubAndProdTpl";
    }

    @RequestMapping("/reloadProd")
    public Object reloadProd(Long ajaxDestId,Long ajaxSubjectId,Model model){
        //根据目的地id和主题id查询景点门票
        List<Ticket> ticket = ticketService.listByDestIdAndThemeId(ajaxDestId,ajaxSubjectId);
        model.addAttribute("tickets", ticket);

        return "ticket/reloadProdTpl";
    }

    @RequestMapping("/detail")
    public Object detail(Long tid,Model model){
        //当前门票
        Ticket ticket = ticketService.getById(tid);
        model.addAttribute("ticket", ticket);
        //价格信息
        model.addAttribute("ticketPrices", ticketDetailService.listByTicketId(ticket.getId()));

        //相关景点推荐
        model.addAttribute("scenicCommends",scenicDetailService.listCommendByTicketId(tid));
        return "ticket/detail";
    }

    @RequestMapping("/addOrder")
    public Object addOrder(Long tid,Model model){
        TicketDetail ticketDetail = ticketDetailService.getById(tid);
        model.addAttribute("ticketPrice", ticketDetail);
        return "ticket/addOrder";
    }

    @RequestMapping("/buyTicket")
    public Object buyTicket(String price, Model model){
        model.addAttribute("price",price);
        return "/ticket/pay";
    }

}
